package com.frikinet.lifeguard.gui;

import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.select;

import java.awt.BorderLayout;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JPanel;

import org.lobobrowser.html.HtmlRendererContext;
import org.w3c.dom.html2.HTMLElement;
import org.w3c.dom.html2.HTMLLinkElement;

import ch.lambdaj.function.matcher.Predicate;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.events.YearMonthSelectedEvent;
import com.frikinet.lifeguard.events.YearSelectedListener;
import com.frikinet.lifeguard.model.Post;
import com.frikinet.lifeguard.model.User;
import com.frikinet.lifeguard.util.Day;
import com.frikinet.lifeguard.util.HibernateUtil;
import com.frikinet.lifeguard.util.WeekNames;
import com.frikinet.lifeguard.util.Year;

public class YearHtmlPanel extends JPanel implements YearSelectedListener
{
	private static final long serialVersionUID = 1L;
	
	private TemplateHtmlPanel htmlPanel;
	
	public YearHtmlPanel()
	{
		super(new BorderLayout());
		
		App.events.addYearSelectedListener(this);
		
		buildGUI();
	}
	
	private void buildGUI()
	{
		htmlPanel = new TemplateHtmlPanel(new MonthLinkBehaviour());
		add(htmlPanel, BorderLayout.CENTER);
	}
	
	@Override
	public void yearSelected(YearMonthSelectedEvent event)
	{
		Map<String, Object> context = new HashMap<String, Object>();
		
		Map<String, Post> posts = new HashMap<String, Post>();
		for(Post post: select(event.getUser().getPosts(), having(on(Post.class).getDate(), new IsYear(event.getYear()))))
		{
			posts.put(Day.DAY_ID_FORMAT.format(post.getDate()), post);
		}
		context.put("posts", posts);

		context.put("weekDays", new WeekNames("E"));
		
		context.put("year", new Year(event.getYear()));
		context.put("user", event.getUser());
		
		htmlPanel.navigate("year", context);
	}
	
	public static class IsYear extends Predicate<Date>
	{
		private int year;
		public IsYear(int year)
		{
			this.year = year;
		}
		@Override
		public boolean apply(Date date)
		{
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			return c.get(Calendar.YEAR)==year;
		}		
	}
	
	public class MonthLinkBehaviour extends HtmlBehaviourAdapter
	{
		private final Pattern linkPattern = Pattern.compile("^(.*?)/(\\d{4})(\\d\\d)$");
		@Override
		public boolean checkElement(HTMLElement element)
		{
			return element instanceof HTMLLinkElement;
		}

		@Override
		public void performLinkClicked(HtmlRendererContext renderer, HTMLElement element, URL url, String target)
		{
			Matcher matcher = linkPattern.matcher(((HTMLLinkElement)element).getAttribute("href"));
			if(matcher.find())
			{
				User user = HibernateUtil.get(User.class, "name", matcher.group(1));
				if(user != null)
				{
					App.events.fireMonthSelected(new YearMonthSelectedEvent(this, user, new Integer(matcher.group(2)), new Integer(matcher.group(3))));
				}
			}
		}
		
	}
}
